package com.mz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.common.dto.GlobalException;
import com.mz.common.dto.PaginationVo;
import com.mz.common.utils.NicknameUtils;
import com.mz.dao.UmsUserInfoDao;
import com.mz.entity.UmsUserInfo;
import com.mz.entity.vo.*;
import com.mz.service.UmsUserInfoService;
import io.jsonwebtoken.lang.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (UmsUserInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 13:38:42
 */
@Service
public class UmsUserInfoServiceImpl  implements UmsUserInfoService {

    @Resource
    private UmsUserInfoDao userInfoDao;

    public IPage getUserList(PaginationVo paginationVo, UserQuery userQuery) throws Exception {

        IPage<UmsUserInfo> page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<UmsUserInfo> lqs = new LambdaQueryWrapper();
        lqs.likeRight( userQuery.getUsername() != null , UmsUserInfo::getUsername, userQuery.getUsername() );
        lqs.eq( userQuery.getRoleId() != null, UmsUserInfo::getRoleId, userQuery.getRoleId() );
        lqs.isNull(UmsUserInfo::getOpenid);
        lqs.orderByDesc(UmsUserInfo::getCreateTime);

        userInfoDao.selectPage(page, lqs);
        page.setRecords(page.getRecords().stream().map(item->{
            item.setPassword(null);
            return item;
        }).collect(Collectors.toList()));
        return page;
    }

    @Override
    @Transactional
    public void addUser(UserVo userVo) throws Exception {
        if( userVo.getId() != null ){
            throw GlobalException.gen(10013, "用户创建接口调用错误");
        }

        //判断账号是否存在
        LambdaQueryWrapper<UmsUserInfo> lqs = new LambdaQueryWrapper<>();
        lqs.eq(UmsUserInfo::getUsername,  userVo.getUsername() );
        UmsUserInfo umsUserInfoInSQL = userInfoDao.selectOne(lqs);
        if( umsUserInfoInSQL != null ){
            throw GlobalException.gen(10013, "用户已存在");
        }

        //如果昵称不存在，则自动生成一个随机昵称
        if( !Strings.hasText(userVo.getNickname())  ){
           String nickname = NicknameUtils.generateNickname();
           userVo.setNickname(nickname);
        }

        UmsUserInfo umsUserInfo = new UmsUserInfo();
        umsUserInfo.setUsername(userVo.getUsername())
            .setPassword(userVo.getPassword())
                .setIsLock(0)
                .setRoleId(userVo.getRoleId())
                .setNickname(userVo.getNickname());

        userInfoDao.insert(umsUserInfo);
    }

    @Override
    public void removeUser(List<Long> idList) throws Exception {

        if( idList == null || idList.size() == 0 ){
            throw GlobalException.gen(10014, "用户id不存在");
        }

        userInfoDao.deleteBatchIds(idList);
    }


    @Override
    @Transactional
    public void updatePassword(PasswordVo passwordVo) throws Exception {

        UmsUserInfo umsUserInfo = new UmsUserInfo();
        umsUserInfo.setId(passwordVo.getUserId());
        umsUserInfo.setPassword(passwordVo.getPassword());
        userInfoDao.updateById(umsUserInfo);
    }


    @Override
    @Transactional
    public void update(UserVo userVo) throws Exception {

        //不能修改 username, password
        UmsUserInfo umsUserInfo = new UmsUserInfo();
        umsUserInfo.setRoleId(userVo.getRoleId())
                .setNickname(userVo.getNickname())
                .setId(userVo.getId());
        userInfoDao.updateById(umsUserInfo);
    }

    @Override
    public IPage getMemberList(PaginationVo paginationVo,  MemberQuery memberQuery) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<UmsUserInfo> lqs = new LambdaQueryWrapper<>();
        lqs.isNotNull(UmsUserInfo::getOpenid);
        lqs.orderByDesc(UmsUserInfo::getCreateTime);

        userInfoDao.selectPage(page, lqs);
        return page;
    }

    @Override
    @Transactional
    public void lockAndUnlock(MemberLockVo memberLockVo) throws Exception {

        LambdaQueryWrapper<UmsUserInfo> lqs = new LambdaQueryWrapper<>();
        lqs.eq(UmsUserInfo::getIsLock, memberLockVo.getIsLock());
        lqs.eq(UmsUserInfo::getId, memberLockVo.getUserId());

        UmsUserInfo umsUserInfo = new UmsUserInfo();
        umsUserInfo.setId(memberLockVo.getUserId())
                        .setIsLock(memberLockVo.getIsLock());

        userInfoDao.updateById(umsUserInfo);
    }

}

